package servlet;

import dao.Db;
import model.User;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
public class AddOne extends GenericServlet {
    public void service(ServletRequest servletRequest, ServletResponse servletResponse)throws ServletException, IOException{
        //设置编码
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setContentType("text/html;charset=UTF-8");
        //收参
        String username=servletRequest.getParameter("username");
        String password=servletRequest.getParameter("password");
        String birthday=servletRequest.getParameter("birthday");
        User user=new User(null,username,password,birthday);
        System.out.println("user="+user);
        //访问数据访问层
        Db db=new Db();
        int i=db.add(user);
        System.out.println("i="+i);
        if (i>0){
            servletRequest.getRequestDispatcher("user").forward(servletRequest,servletResponse);
        }else {
            System.out.println("删除失败");
        }
    }
}
